/************************************************************************************/
/*  Program name CAPFAIMP_DEPRN_PERIOD.sql                                        	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 17 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_DEPRN_PERIOD';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_DEPRN_PERIODS;

BEGIN
	UPDATE
		FA_DEPRN_PERIODS
	SET	
		BOOK_TYPE_CODE	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_DEPRN_PERIODS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN		
			INSERT INTO FA_DEPRN_PERIODS
			VALUES
			(
			C1.Book_Type_Code,
			C1.Period_Name,
			C1.Period_Counter,
			C1.Fiscal_Year,
			C1.Period_Num,
			C1.Period_Open_Date,
			C1.Period_Close_Date,
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Depreciation_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Retirement_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Reclass_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Transfer_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Addition_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Adjustment_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Deferred_Deprn_Batch_ID),
			C1.Calendar_Period_Open_Date,
			C1.Calendar_Period_Close_Date,
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_Addition_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_Adjustment_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_Reclass_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_Retirement_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_Reval_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_Transfer_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Reval_Batch_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.Deprn_Adjustment_Batch_ID),
			C1.Deprn_Run
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/